<?php


/**
 * 图片上传管理
 * Enter description here ...
 * @author Admin
 *
 */
class AccessoryAction extends CommonAction{
	
	
	
	/**
	 * 上传图片
	 * (多个文件)
	 * Enter description here ...
	 */
	public function imgUpload(){
		
		 if (!empty($_FILES)) {
		 	import("@.ORG.UploadFile");
        	$upload = new UploadFile();
			$upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        	//设置附件上传目录
       		$upload->savePath = UPLOADIMG;    //common.php中定义上传目录
	            //设置上传文件大小
	        $upload->maxSize = 3292200;
	        //设置上传文件类型
	
	        //设置需要生成缩略图，仅对图像文件有效
	        $upload->thumb = true;
	        // 设置引用图片类库包路径
	        $upload->imageClassPath = '@.ORG.Image';
	        //设置需要生成缩略图的文件后缀
	        $upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
	        //设置缩略图最大宽度
	        $upload->thumbMaxWidth = '400,100';
	        //设置缩略图最大高度
	        $upload->thumbMaxHeight = '275,75';
	        //设置上传文件规则
	        $upload->saveRule = uniqid;
	        //删除原图
	        $upload->thumbRemoveOrigin = false;
	        if (!$upload->upload()) {
	            //捕获上传异常
	            $this->error($upload->getErrorMsg());
	        } else {
	            //取得成功上传的文件信息
	            $uploadList = $upload->getUploadFileInfo();
	            import("@.ORG.Image");
	            //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
	            $path = $uploadList[0]['savepath'].'m_'.$uploadList[0]['savename'];
	            Image::water($path, __PUBLIC__.'/images/watermark.png');
	        }
	        $model = M('Accessory');
	        
	        //保存当前数据对象
	        $data['path'] = 'm_'.$uploadList[0]['savename'];
	        $data['thumbnail'] = 's_'.$uploadList[0]['savename'];
	        
	        $data['type'] = 'img';
	        $data['title']= '';
	        $data['size']= $uploadList[0]['size'];
	        $data['uploadtime'] = time();
	        $list = $model->add($data);
	        if ($list !== false) {
	        	$severdata['FILEID'] = __ROOT__."/Public/accessory/img/".'s_'.$uploadList[0]['savename'];
	        	$severdata['id'] = $list;
	            echo json_encode($severdata);
	        } else {
	            echo "ERROR:invalid upload";
	        } 
        } 
		
	}
	
	
	
	
	
	
	//上传文件
	public function fileUpload(){
		if (!empty($_FILES)) {
		 	import("@.ORG.UploadFile");
        	$upload = new UploadFile();
			$upload->allowExts = explode(',', 'doc,docx,ppt,pdf,chm,zip,rar,xls');
        	//设置附件上传目录
       		$upload->savePath = UPLOADIMG;
        } 
	}
	
	
	
	//删除图片
	public function delImg(){
		$id = GetNum($_GET['id']);
		if(!empty($id)){
			$model = M('Accessory');
			if($model->delete($id)){
				$data['state']= "success";
				echo json_encode($data);
			}else {
				$data['state']= "error";
				echo json_encode($data);
			}
		}
	}
	
	
	
	/**
	 * 推荐上传
	 * （单个文件）
	 * Enter description here ...
	 * @param unknown_type $files
	 * @param unknown_type $width
	 * @param unknown_type $height
	 */
	public function recommendUpload($files,$width,$height){
		 	
		 	import("@.ORG.UploadFile");
        	$upload = new UploadFile();
			$upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        	//设置附件上传目录
       		$upload->savePath = UPLOADIMG;
	            //设置上传文件大小
	        $upload->maxSize = 3292200;
	        //设置上传文件类型
	
	        //设置需要生成缩略图，仅对图像文件有效
	        $upload->thumb = true;
	        // 设置引用图片类库包路径
	        $upload->imageClassPath = '@.ORG.Image';
	        //设置需要生成缩略图的文件后缀
	        $upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
	        //设置缩略图最大宽度
	        $upload->thumbMaxWidth = $width;
	        //设置缩略图最大高度
	        $upload->thumbMaxHeight = $height;
	        //设置上传文件规则
	        $upload->saveRule = uniqid;
	        //删除原图
	        $upload->thumbRemoveOrigin = false;
	        
	        if (!$fileInfo = $upload->uploadOne($_FILES["{$files}"])) {
	            //捕获上传异常
	            $this->error($upload->getErrorMsg());
	            return false;
	        } else {
	            //取得成功上传的文件信息
	            
	            $model = M('Accessory');      //....上传信息保存到附件表
	      		  //保存当前数据对象
		        $data['path'] = 'm_'.$fileInfo[0]['savename'];
		        $data['thumbnail'] = 's_'.$fileInfo[0]['savename'];
		        $data['type'] = 'img';
		        $data['title']= '';
		        $data['size']= $fileInfo[0]['size'];
		        $data['uploadtime'] = time();
		        $list = $model->add($data);
		        
		        return $list;
	        }
	        return false;
		
	}
	
	
	
	
	
	
	
	
	/**
	 * 上传文件
	 *  （单个文件--不保存信息到附件表）
	 * Enter description here ...
	 */
	public function upFile($pathname){
		Log::write(".......开始上片.......",Log::DEBUG);
		
		import('@.Org.UploadFile');
		$upload=new UploadFile();
		
		$upload->maxSize='3292200';  //是指上传文件的大小，默认为-1,不限制上传文件大小bytes
		$upload->savePath=UPLOADIMGEEXTEND.$pathname.'/';     // __ROOT__."/Public/accessory/upload/Activity";
		
		$upload->saveRule=uniqid;    //上传文件的文件名保存规则  time uniqid  com_create_guid  uniqid
		$upload->uploadReplace=true;     //如果存在同名文件是否进行覆盖
		$upload->allowExts=array('jpg','jpeg','png','gif');     //准许上传的文件后缀
		$upload->allowTypes=array('image/png','image/jpg','image/pjpeg','image/gif','image/jpeg');  //检测mime类型
		$upload->thumb=false;   //是否开启图片文件缩略
		$upload->thumbMaxWidth='300,500';  //以字串格式来传，如果你希望有多个，那就在此处，用,分格，写上多个最大宽
		$upload->thumbMaxHeight='200,400';	//最大高度
		$upload->thumbPrefix='s_,m_';//缩略图文件前缀。小图或中途
		$upload->thumbRemoveOrigin=false;  //如果生成缩略图，是否删除原图
		
		if($upload->upload()){
			//局部变量，你可以在此处，保存到一个超全局变量当中去进行返回
			$info=$upload->getUploadFileInfo();    //上传信息不保存到附件表
			
			Log::write(".......上传完毕.......",Log::DEBUG);
					
			return $info;
		}else{
			//是专门来获取上传的错误信息的
			$this->error($upload->getErrorMsg());
		}
		
	
			
	}
	
	
	
	
	
	
	
	
	
	
	
}
?>